package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.mapper.UserMapper;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.vo.UserVO;
import lombok.val;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IUserService;
import com.example.mybatisplusdemo.model.domain.User;


/**
 *
 *  前端控制器
 *
 *
 * @author lqj
 * @since 2025-06-22
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private IUserService userService;

    @Autowired
    private UserMapper userMapper;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<User> getById(@PathVariable("id") Long id)throws Exception {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }

    @GetMapping("getId")
    public User getUserById(String username){
        User user = userMapper.selectByName(username);
        return user;
    }

    @GetMapping("getAddresses")
    public User getAddressesByUsername(User u){
        UserVO user = (UserVO) userMapper.selectAddressesByName(u.getUsername());
        return user;
    }

    @PostMapping("test")
    public User getUserByNamePost(User user){
        User user1 = userMapper.selectByName(user.getUsername());
        return user1;
    }

    @GetMapping("count")
    public JsonResponse<Long> getUserById()throws Exception {
        Long count= userService.count();
        return JsonResponse.success(count);
    }

    @PostMapping("login")
    public JsonResponse login(@RequestBody User user)throws Exception{
        User login = userService.login(user);
        return JsonResponse.success(login);
    }



}

